package com.example.nkj.feather_android;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    //定义图片整型数组
    private int[] mImages={
            R.drawable.ble,
            R.drawable.ble_scan,
            R.drawable.ble_adv,
            R.drawable.wifi,
            R.drawable.usb2serial,
            R.drawable.sensor,
            R.drawable.fly,
            R.drawable.htu,
            R.drawable.add,
    };

    private String[] mTexts={
            "BLE",
            "BLE SCAN",
            "BLE ADV",
            "WIFI",
            "USB",
            "SENSOR",
            "FLY",
            "HTU",
            "ADD",
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //实例化GridView
        GridView mGridView=(GridView) findViewById(R.id.gridview_main);
        // 生成动态数组，并且传入数据
        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();

        for(int i = 0; i < mImages.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", mImages[i]);// 添加图像资源的ID
            map.put("ItemText", mTexts[i]);// 按序号做ItemText
            lstImageItem.add(map);
        }
        //构建一个适配器
        SimpleAdapter simple = new SimpleAdapter(this, lstImageItem, R.layout.gridviewitem_main, new String[] { "ItemImage", "ItemText" }, new int[] {R.id.ItemImage, R.id.ItemText});
        mGridView.setAdapter(simple);

        //添加选择项监听事件
        mGridView.setOnItemClickListener(new GridView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                switch((int)id)
                {
                    case 1:
                        startActivity(new Intent(MainActivity.this,ble_scan.class));
                        finish();
                        break;
                    case 2:
                        startActivity(new Intent(MainActivity.this,ble_adv.class));
                        finish();
                        break;

                    case 6:
                        startActivity(new Intent(MainActivity.this,fly.class));
                        finish();
                        break;

                    case 7:
                        startActivity(new Intent(MainActivity.this,htu_ble_scan_adv.class));
                        finish();
                        break;

                    default:
                        Toast toast = Toast.makeText(getApplicationContext(), "Unsupported functions", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.BOTTOM, 0, 0);
                        toast.show();
                        break;
                }
            }
        });

    }

//    public void onMainButtonClick(View view)
//    {
//        switch(view.getId())
//        {
//            case R.id.button_ble_adv:
//                startActivity(new Intent(MainActivity.this,ble_adv.class));
//                finish();
//                break;
//
//            case R.id.button_ble_scan:
//                startActivity(new Intent(MainActivity.this,ble_scan.class));
//                finish();
//                break;
//        }
//    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
            case KeyEvent.KEYCODE_MENU:
                //Toast.makeText(MainActivity.this, "菜单键点击", Toast.LENGTH_SHORT).show();
                break;
            case KeyEvent.KEYCODE_BACK:
                //Toast.makeText(MainActivity.this, "返回键点击", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
